<?php
// 密码验证
$correctPassword = '123456';
if (!isset($_POST['password']) || $_POST['password'] !== $correctPassword) {
    echo '<!DOCTYPE html>
<html>
<head>
    <title>图片格式转换工具</title>
    <style>
        body {
            font-family: "Comic Sans MS", cursive, sans-serif;
            background-color: #ffebee;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            text-align: center;
            background-color: white;
            padding: 30px;
            border-radius: 20px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        h1 {
            color: #ff6b6b;
            margin-bottom: 20px;
        }
        input[type="password"] {
            padding: 10px;
            border: 2px solid #ffb6c1;
            border-radius: 10px;
            font-size: 16px;
            margin-bottom: 15px;
        }
        input[type="submit"] {
            background-color: #ff6b6b;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 10px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        input[type="submit"]:hover {
            background-color: #ff4757;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>✨ 图片格式转换工具 ✨</h1>
        <form method="post">
            请输入密码: <input type="password" name="password" />
            <input type="submit" value="提交" />
        </form>
    </div>
</body>
</html>';
    exit;
}

// 检查GD库是否可用
if (!extension_loaded('gd')) {
    die('GD库未安装，无法进行图片格式转换');
}

// 图片目录路径
$imageDir = __DIR__ . '/pc/';
if (!is_dir($imageDir)) {
    die('图片目录不存在');
}

// 支持的图片格式
$formats = ['webp', 'jpg', 'png'];

// 遍历图片目录
$files = scandir($imageDir);
foreach ($files as $file) {
    if ($file === '.' || $file === '..') continue;
    
    $filePath = $imageDir . $file;
    $fileInfo = pathinfo($filePath);
    
    // 只处理图片文件
    if (!isset($fileInfo['extension']) || !in_array(strtolower($fileInfo['extension']), $formats)) {
        continue;
    }
    
    // 检查并生成缺少的格式
    foreach ($formats as $format) {
        if ($fileInfo['extension'] === $format) continue;
        
        $newPath = $imageDir . $fileInfo['filename'] . '.' . $format;
        if (!file_exists($newPath)) {
            // 使用GD库转换图片格式
            $image = null;
            switch ($fileInfo['extension']) {
                case 'jpg':
                case 'jpeg':
                    $image = imagecreatefromjpeg($filePath);
                    break;
                case 'png':
                    $image = imagecreatefrompng($filePath);
                    break;
                case 'webp':
                    $image = imagecreatefromwebp($filePath);
                    break;
            }
            
            if ($image) {
                $success = false;
                switch ($format) {
                    case 'jpg':
                        $success = imagejpeg($image, $newPath, 90);
                        break;
                    case 'png':
                        $success = imagepng($image, $newPath, 9);
                        break;
                    case 'webp':
                        $success = imagewebp($image, $newPath, 90);
                        break;
                }
                
                imagedestroy($image);
                if ($success) {
                    echo "已生成: " . basename($newPath) . "<br>";
                }
            }
        }
    }
}

echo '图片格式转换完成!';
?>